package com.cxl.parser;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpUriRequest;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;
import org.htmlparser.Node;
import org.htmlparser.NodeFilter;
import org.htmlparser.Tag;
import org.htmlparser.filters.AndFilter;
import org.htmlparser.filters.HasAttributeFilter;
import org.htmlparser.filters.HasParentFilter;
import org.htmlparser.filters.TagNameFilter;
import org.htmlparser.util.NodeList;

public class Grab360Buy extends Grab implements InfoGrab<String> {
	


	public NodeFilter getFilter() {
		// TODO Auto-generated method stub
		return null;
	}

	public NodeFilter getPictureFilter() {
		return new AndFilter(new TagNameFilter("img"),
				new HasAttributeFilter("width", "350"));
	}

	/*
	 * 关键字搜索的filter
	 */
	public NodeFilter getSearchUrlFiter1() {
		
		NodeFilter filter4 = new AndFilter(new TagNameFilter("ul"),
				new HasAttributeFilter("class", "list-h clearfix"));
		
		NodeFilter filter2 = new AndFilter(new TagNameFilter("li"),
				new HasParentFilter(filter4));
		
		NodeFilter filter1 = new AndFilter(new TagNameFilter("div"),
				new HasAttributeFilter("class", "p-img"));
		
		NodeFilter filter = new AndFilter(filter1,
				new HasParentFilter(filter2));

		NodeFilter filter3 = new AndFilter(new TagNameFilter("a"),
				new HasParentFilter(filter));
		
		return filter3;
	}

	/**
	 * 由目录进去查找商品url的filter
	 */
public NodeFilter getSearchUrlFiter() {
		
		NodeFilter filter4 = new AndFilter(new TagNameFilter("ul"),
				new HasAttributeFilter("class", "list-h"));
		
		NodeFilter filter2 = new AndFilter(new TagNameFilter("li"),
				new HasParentFilter(filter4));
		
		NodeFilter filter1 = new AndFilter(new TagNameFilter("div"),
				new HasAttributeFilter("class", "p-img"));
		
		NodeFilter filter = new AndFilter(filter1,
				new HasParentFilter(filter2));

		NodeFilter filter3 = new AndFilter(new TagNameFilter("a"),
				new HasParentFilter(filter));
		
		return filter3;
	}


public List<String> searchGoodsUrl(String url) {
	List<String> list = new ArrayList<String>();

	NodeFilter filter =null;
	if(url.contains("keyword"))
		filter= getSearchUrlFiter1();
	else
	filter= getSearchUrlFiter();
		
		NodeList  listnode = getNodesByFilter(url, filter, -1);

		if (listnode.size() > 0) {
			for (int i = 0; i < (listnode.size()); i++) {
				Node textnode = listnode.elementAt(i);
				Tag a = (Tag) textnode;
				//System.out.println(a.getAttribute("href"));
				list.add(a.getAttribute("href"));
			}
		}
		
	return list;
}

	/**
	 * 获得下一页URL
	 */
	public String getNextUrl(String url) {
		NodeList nodelist = getNodesByFilter(url, getNextFilter(), 1);
		if (nodelist.size() == 0)
			return null;
		Tag a = (Tag) nodelist.elementAt(0);
		String href=a.getAttribute("href");
		if(href.contains("Search?keyword="))
		{
			try
			{
			//  http://search.360buy.com/Search?keyword=电风扇&cid=751&psort=3&ev=&page=2  需要转码
			String oldkey=href.substring(href.indexOf("=")+1, href.indexOf("&"));
			String key=java.net.URLEncoder.encode(oldkey,"GB2312");
			
			href=href.replace(oldkey, key);
			
			}catch(Exception ex)
			{
				ex.printStackTrace();
			}
			return "http://search.360buy.com/"+href;
		}
		else
		return "http://www.360buy.com/products/"+href;
	}
	
	
	public Double getPrice(String url){
		try {
		Double db =new Double(0);
		String beginkey = null;
		String endKey = null;
		int length = 0;
		
		beginkey = "京东价";
		length = 5;
		endKey = "。";

		HttpClient httpClient = new DefaultHttpClient();
		HttpUriRequest request = new HttpGet(url);
		HttpResponse response;
		
			response = httpClient.execute(request);
		
		HttpEntity entity = response.getEntity();
		String content = EntityUtils.toString(entity);
		int num = content.indexOf(beginkey);
		content = content.substring(num + length);
		num = content.indexOf(endKey);
		return getMoney(content.substring(0, num));
		} catch (ClientProtocolException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
		
	}
	
	public String getContent(String url) {
		StringBuffer bufStr=new StringBuffer();
		bufStr.append("<P><IMG src=\"http://img03.taobaocdn.com/imgextra/i3/69131440/T2I.8QXe8XXXXXXXXX_!!69131440.jpg\"></P>" +
//				"<style>" +
//				".Ptable {BACKGROUND: #ccc}" +
//				".Ptable TH {PADDING-RIGHT: 5px; PADDING-LEFT: 5px; BACKGROUND: #eef7fe; PADDING-BOTTOM: 5px; PADDING-TOP: 5px; TEXT-ALIGN: right}"+
//				".Ptable TD {PADDING-RIGHT: 5px; PADDING-LEFT: 5px; BACKGROUND: #fff; PADDING-BOTTOM: 2px; PADDING-TOP: 2px}"+
//				".Ptable .tdTitle {BACKGROUND: #f5fafe; WIDTH: 110px; TEXT-ALIGN: right}" +
//				"</style>" +
				"");
		NodeList nodelist = getNodesByFilter(url, getContentFilter(), -1);
		if (nodelist.size() == 0||nodelist.size() == 1)
			return null;
		bufStr.append("<div>"+nodelist.elementAt(1).toHtml()+"</div>");
		
		nodelist = getNodesByFilter(url, getContentFilter1(), 1);
		if (nodelist.size() != 0)
		{
		String table="<div>"+nodelist.elementAt(0).toHtml()+"</div>";
		table =table.replace("class=\"Ptable\"","background=\"#ccc\"");
		table=table.replace("<th>", "<th style='PADDING-RIGHT: 5px; PADDING-LEFT: 5px; BACKGROUND: #eef7fe; PADDING-BOTTOM: 5px; PADDING-TOP: 5px; TEXT-ALIGN: right'>");
		table=table.replace("<td>","<td style='PADDING-RIGHT: 5px; PADDING-LEFT: 5px; BACKGROUND: #fff; PADDING-BOTTOM: 2px; PADDING-TOP: 2px' >");
		table=table.replace("td class=\"tdTitle\"","td style='BACKGROUND: #f5fafe; WIDTH: 110px; TEXT-ALIGN: right'");		
		table=table.replace("th class=\"tdTitle\"","th style='BACKGROUND: #f5fafe; WIDTH: 110px; TEXT-ALIGN: right'");		
		bufStr.append(table);
		}
		
		String myDsc=""+
		"<DIV style=\"CLEAR: none; MARGIN-BOTTOM: 2em; LINE-HEIGHT: 1.4em\">"+
		"<H2 style=\"PADDING-RIGHT: 0px; PADDING-LEFT: 0px; FONT-SIZE: medium; PADDING-BOTTOM: 0px; MARGIN: 0px 0px 0.25em; COLOR: #e47911; PADDING-TOP: 0px\">"+
		"<HR style=\"BORDER-TOP: #999999 1px dashed; MARGIN: 3px 0px; COLOR: #ffffff; BORDER-RIGHT-STYLE: none; BORDER-LEFT-STYLE: none; HEIGHT: 1px; BACKGROUND-COLOR: white; BORDER-BOTTOM-STYLE: none\">"+
		"</H2></DIV></SPAN></SPAN></STRONG><STRONG>尚生活服务承诺：</STRONG><BR>尚生活向您保证所售商品均为正品行货，自带机打发票，与商品一起寄送。凭质保证书及发票，可享受全国联保服务，与您亲临商场选购的商品享受相同的质量保证。<BR>尚生活还为您提供具有竞争力的商品价格和<A target=_blank>免运费政策</A>，请您放心购买！ </DIV></DIV>"+
		"<DIV><STRONG><FONT color=#cc3300>声明:</FONT></STRONG>因厂家会在没有任何提前通知的情况下更改产品包装、产地或者一些附件，本司不能确保客户收到的货物与商城图片、产地、附件说明完全一致。只能确保为原厂正货！并且保证与当时市场上同样主流新品一致。若本商城没有及时更新，请大家谅解！</DIV>"+
		"<DIV>&nbsp;</DIV>"+
		"<DIV><STRONG><FONT color=#804000>关于快递：合作快递和普通快递有所不同，部分快递网上无法查询订单状态，如需查询可旺旺联系我，非常乐意代劳！</FONT></STRONG></DIV>"+
		"<DIV><STRONG><FONT color=#804000>提示：本店包邮产品包邮地区仅限各城市市区，如您所在位置较偏远购买前请先咨询我替您查询下。</FONT></STRONG></DIV>";
		bufStr.append(myDsc);
		bufStr.append("<DIV><IMG src=\"http://img04.taobaocdn.com/imgextra/i4/69131440/T2EDJOXeNbXXXXXXXX_!!69131440.jpg\" align=middle></DIV>");
		String reStr=bufStr.toString().replace(",", ".").replace("，", ".");
		
		String reg ="[\n-\r]";
		   Pattern p = Pattern.compile(reg);
		   Matcher m = p.matcher(reStr);
		   reStr = m.replaceAll("");
		
		   reg ="[\t]";
		   p = Pattern.compile(reg);
		  m = p.matcher(reStr);
		  reStr = m.replaceAll("");
		
		System.out.println(reStr);
		System.out.println("--------------------------------------------------------------------------------------");
		return reStr;
	}
	

	public NodeFilter getNextFilter() {
		return new AndFilter(new TagNameFilter("a"),
				new HasAttributeFilter("class", "next"));
	}

	public NodeFilter getContentFilter() {
		return new AndFilter(new TagNameFilter("div"),
				new HasAttributeFilter("class", "content"));
	}
	
	public NodeFilter getContentFilter1() {
		return new AndFilter(new TagNameFilter("table"),
				new HasAttributeFilter("class", "Ptable"));
	}

	

	public NodeFilter getContentFilter2() {
		// TODO Auto-generated method stub
		return null;
	}

	

	public NodeFilter getTitleFilter() {
		NodeFilter filter1 = new AndFilter(new TagNameFilter("div"),
				new HasAttributeFilter("id", "name"));
		
		NodeFilter filter2 = new AndFilter(new TagNameFilter("h1"),
				new HasParentFilter(filter1));
		return filter2;
	}

	public NodeFilter getNotExistsFilter() {
		// TODO Auto-generated method stub
		return null;
	}

	public NodeFilter getPtypeFirstFilter() {
		NodeFilter filter1 = new AndFilter(new TagNameFilter("div"),
				new HasAttributeFilter("class", "m"));
		return filter1;
	}

	
	public NodeFilter getPtypeOneFilter() {
		
		NodeFilter filter1 = new AndFilter(new TagNameFilter("div"),
				new HasAttributeFilter("class", "mt"));
		
		NodeFilter filter2 = new AndFilter(new TagNameFilter("h2"),
				new HasParentFilter(filter1));
		NodeFilter filter3 = new AndFilter(new TagNameFilter("a"),
				new HasParentFilter(filter2));
		return filter3;
	}
	
	/**
	 * <div class="mc">
	                         <dl class='fore'><dt>
	 */
	public NodeFilter getPtypeSecondFilter() {
	
		NodeFilter filter1 = new AndFilter(new TagNameFilter("div"),
				new HasAttributeFilter("class", "mc"));
		
		NodeFilter filter2 = new AndFilter(new TagNameFilter("dl"),
				new HasParentFilter(filter1));
		return filter2;
	
	}
	
	public NodeFilter getPtypeTwoFilter() {
		NodeFilter filter1=new TagNameFilter("dt");
		NodeFilter filter2 = new AndFilter(new TagNameFilter("a"),
				new HasParentFilter(filter1));
		return filter2;
	}
	
	public NodeFilter getPtypeTwoFilter1() {
		NodeFilter filter1=new TagNameFilter("dt");
		return filter1;
	}

	

	public NodeFilter getPtypeThirdFilter() {
		return  new TagNameFilter("dd");
	}
	
	
	public NodeFilter getPtypeThreeFilter() {
		
		NodeFilter filter1 =  new TagNameFilter("em");
		
		NodeFilter filter2 = new AndFilter(new TagNameFilter("a"),
				new HasParentFilter(filter1));
		return filter2;
	
	}
	

	

	
	
	
	public NodeFilter getBrandFilter()
	{
		
		NodeFilter filter1 = new AndFilter(new TagNameFilter("div"),
				new HasAttributeFilter("class", "crumb"));
		
		NodeFilter filter2 = new AndFilter(new TagNameFilter("a"),
				new HasParentFilter(filter1));
		return filter2;
	}
	
	
	public String getBrand(String url) {
		NodeList list=getNodesByFilter(url,getBrandFilter(),-1);
		StringBuffer title = new StringBuffer();
		if (list.size() > 0) {
			Node textnode = list.elementAt(list.size()-2);
			try {
				title.append(
						new String(textnode.toPlainTextString()
								.getBytes("UTF-8"))).toString();
			} catch (UnsupportedEncodingException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		return title.toString();
	}
	
	
	public String getXingHao(String url) {
		NodeList list=getNodesByFilter(url,getBrandFilter(),-1);
		StringBuffer title = new StringBuffer();
		if (list.size() > 0) {
			Node textnode = list.elementAt(list.size()-1);
			try {
				title.append(
						new String(textnode.toPlainTextString()
								.getBytes("UTF-8"))).toString();
			} catch (UnsupportedEncodingException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		return title.toString();
	}
	
	//获得每个模块
	public NodeList getFirstNodeList(String url)
	{
		NodeList nodelist=getNodesByFilter(url,getPtypeFirstFilter(),-1) ;
		return nodelist;
	}
	
	//获得第一级
	public Node getFirstNodes(Node node)
	{
			NodeList nodelist=new NodeList();
			node.collectInto(nodelist, getPtypeOneFilter());
			return nodelist.elementAt(0);
	}
	
	//获得第二级 模块
	public NodeList getSecondNodeList(Node firstNode)
	{
		NodeList nodelist=new NodeList();
		firstNode.collectInto(nodelist,getPtypeSecondFilter());
		return nodelist;
	}
	
	
	//获得第2级
	public Node getSecondNodes(Node node)
	{
		NodeList nodelist=new NodeList();
		node.collectInto(nodelist, getPtypeTwoFilter());
		return nodelist.elementAt(0);
	}
	
	



	
	public static void main(String[] args) {
		
		String url="http://www.360buy.com/allSort.aspx";
		Grab360Buy grab=new Grab360Buy();
		NodeList listNode=grab.getNodesByFilter(url,grab.getPtypeFirstFilter(),-1) ;//第一层
		int i=0;
		while(listNode.size()>0)
		{
			Node node=listNode.elementAt(0);
			Node nodeFirst=grab.getNextNodeListByFilter(node,grab.getPtypeOneFilter()).elementAt(0); //第1层结果
			NodeList nodeSecondList=grab.getNextNodeListByFilter(node,grab.getPtypeSecondFilter());//第2层 
			
			while(nodeSecondList.size()>0)
			{
				Node node1=nodeSecondList.elementAt(0);
				
				Node nodeSecond=grab.getNextNodeListByFilter(node,grab.getPtypeTwoFilter()).elementAt(0); //第2层结果
				
				NodeList nodethirdList=grab.getNextNodeListByFilter(node1,grab.getPtypeThirdFilter());//第3层 
					while(nodethirdList.size()>0)
					{
						Node node2=nodethirdList.elementAt(0);
						NodeList lastList=grab.getNextNodeListByFilter(node2,grab.getPtypeThreeFilter());
						while(lastList.size()>0)
						{
							Node node3=lastList.elementAt(0);	
							
							Tag a = (Tag) node3;
							System.out.println(a.getAttribute("href"));
							//System.out.println(node3);
							lastList.remove(0);
							
						}
						nodethirdList.remove(0);
						i++;
					}
				
				nodeSecondList.remove(0);
			}
		
			listNode.remove(0);
		}
		
		System.out.println(i);
	//	System.out.println(list.size());
	}
	
	
}
